//
//  ForgetPasswordViewController.m
//  JunZeWristband
//
//  Created by Kendama on 2017/8/16.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "ForgetPasswordViewController.h"
#import "ForgetPasswordView.h"
#import "PasswordTextFieldView.h"
#import "PhoneTextFieldView.h"
#import "VerifyTextFieldView.h"

#import "PublicForgetPwdApi.h"
#import "PublicGetForgetCodeApi.h"

@interface ForgetPasswordViewController ()

@property(nonatomic,strong) ForgetPasswordView *containerView;

@end

@implementation ForgetPasswordViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    
}

- (void)refreshBaseControlValue
{
    
}

#pragma mark - 点击事件
- (void)getVerifyCode
{
    if (![self.containerView.phoneView.phoneTextField.text isMobileNumber]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码..."];
        return;
    }
    
    [self getVerifyCodeNumber];
}

// 获得验证码
- (void)getVerifyCodeNumber {
    [SVProgressHUD showInfoWithStatus:@"正在获取验证码..."];
    
    PublicGetForgetCodeApi * codeApi = [[PublicGetForgetCodeApi alloc] initWithDic:@{@"username":self.containerView.phoneView.phoneTextField.text}];
    [codeApi startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary * codeDic = [request responseJSONObject];
        NSLog(@"code Dic == %@ ",codeDic);
        //        [codeApi analyseResponseObject];

        if ([codeDic[@"errcode"] intValue] == 0) {
            [SVProgressHUD showInfoWithStatus:codeDic[@"errmsg"]];
            [self setVerifyBtnTimer];
            SAVE_OBJECT(codeDic[@"vcode"], @"vcode");
        }else{
            [SVProgressHUD showErrorWithStatus:codeDic[@"errmsg"]];
        }

    } failure:^(YTKBaseRequest *request) {
        [SVProgressHUD showErrorWithStatus:@"请求超时"];
    }];
}

// 设置定时器
- (void)setVerifyBtnTimer {
    __block int code_out = 60;
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        if (code_out <= 0) {
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [_containerView.verifyView.verifyButton setTitle:@"获取验证码" forState:0];
                [_containerView.verifyView.verifyButton setTitle:@"60秒后获取" forState:UIControlStateDisabled];
                _containerView.verifyView.verifyButton.enabled = YES;
                code_out = 60;
            });
        } else {
            _containerView.verifyView.verifyButton.enabled = NO;
            NSString * time = [NSString stringWithFormat:@"%d %@",code_out,@"秒后获取"];
            dispatch_async(dispatch_get_main_queue(), ^{
                [_containerView.verifyView.verifyButton setTitle:time forState:UIControlStateDisabled];
            });
            code_out -- ;
        }
    });
    dispatch_resume(timer);
}

- (void)didClickCommitButton
{
    if (![self.containerView.phoneView.phoneTextField.text isMobileNumber]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码..."];
        return;
    }
    
    if (![self.containerView.passwordView.passwordTextField.text isRegexPassword]) {
        [SVProgressHUD showErrorWithStatus:@"请输入符合规定的密码"];
        return;
    }
    
    if (self.containerView.verifyView.verifyTextField.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"亲，还未输入验证码..."];
        return;
    }
    
    if (self.containerView.verifyView.verifyTextField.text.length < 6 || self.containerView.verifyView.verifyTextField.text.length > 6) {
        [SVProgressHUD showErrorWithStatus:@"亲，请输入6位验证码..."];
        return;
    }
    
    [self confirmActionStart];
}

// 开始确定
- (void)confirmActionStart {
    PublicForgetPwdApi * forgetPwdApi = [[PublicForgetPwdApi alloc] initWithDic:@{@"username":self.containerView.phoneView.phoneTextField.text,@"password":self.containerView.passwordView.passwordTextField.text,@"vcode":self.containerView.verifyView.verifyTextField.text}];
    [forgetPwdApi dtDefaultInterceptStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        NSDictionary * dic = [forgetPwdApi responseJSONObject];
        
        [SVProgressHUD showSuccessWithStatus:dic[@"errmsg"]];
        if (self.forgetBlock)
        {
            self.forgetBlock(self.containerView.phoneView.phoneTextField.text, self.containerView.passwordView.passwordTextField.text);
        }
        [NSThread sleepForTimeInterval:1];
        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:nil];

}

#pragma mark - 事件处理

#pragma mark - 代理


#pragma mark - 通知处理

#pragma mark - 初始化
- (ForgetPasswordView *)containerView
{
    if(!_containerView)
    {
        UIView *superView = self.view;
        _containerView = [[ForgetPasswordView alloc]init];
        [superView addSubview:_containerView];
        [_containerView.verifyView.verifyButton addTarget:self action:@selector(getVerifyCode) forControlEvents:UIControlEventTouchUpInside];
        [_containerView.registerButton addTarget:self action:@selector(didClickCommitButton) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _containerView;
}

- (void)setNavigationStyle
{
    [self setNavigationTitle:@"忘记密码" titleColor:[UIColor whiteColor]];
}

- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(superView);
    }];
}

@end
